﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace HideList
{
    public partial class MyListControl : UserControl, INotifyPropertyChanged
    {
        private ObservableCollection<TestItem> _testItems;
        public ObservableCollection<TestItem> TestItems
        {
            get { return _testItems; }
            set
            {
                _testItems = value;
                OnPropertyChanged();
            }
        }

        public MyListControl()
        {
            InitializeComponent();
            DataContext = this;

            // 初始化测试数据
            TestItems = new ObservableCollection<TestItem>
            {
                new TestItem { TestProject = "项目1", PresetValue = "值1", TestStatus = "成功" },
                new TestItem { TestProject = "项目2", PresetValue = "值2", TestStatus = "失败" },
                new TestItem { TestProject = "项目3", PresetValue = "值3", TestStatus = "成功" },
                new TestItem { TestProject = "项目4", PresetValue = "值4", TestStatus = "失败" },
                new TestItem { TestProject = "项目5", PresetValue = "值5", TestStatus = "成功" }
            };
        }

        private void HideSuccessButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in TestItems)
            {
                item.IsVisible = (item.TestStatus != "成功");
            }
        }

        private void ShowAllButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in TestItems)
            {
                item.IsVisible = true;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class TestItem : INotifyPropertyChanged
    {
        private string _testProject;
    public string TestProject
    {
        get { return _testProject; }
        set
        {
            _testProject = value;
            OnPropertyChanged();
        }
    }

    private string _presetValue;
    public string PresetValue
    {
        get { return _presetValue; }
        set
        {
            _presetValue = value;
            OnPropertyChanged();
        }
    }

    private string _testStatus;
    public string TestStatus
    {
        get { return _testStatus; }
        set
        {
            _testStatus = value;
            OnPropertyChanged();
        }
    }

    private bool _isVisible = true;
    public bool IsVisible
    {
        get { return _isVisible; }
        set
        {
            _isVisible = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
}